package com.aspirecn.core.datacache.useragent;

import java.io.File;
//import java.lang.reflect.InvocationTargetException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

//import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.log4j.Logger;

import com.aspire.core.match.ua.object.DeviceOS;
import com.aspirecn.core.commons.Configuration;
import com.aspirecn.core.datacache.BaseLocalCache;
import com.csvreader.CsvReader;

/**
 * deviceos数据缓存
 * 
 * @author wangxinning
 * 
 */
public class DeviceOSCache extends BaseLocalCache {

	public final static Logger logger = Logger.getLogger(DeviceOSCache.class);

	private Map<Integer, DeviceOS> deviceOSMap = new HashMap<Integer, DeviceOS>();

	private static DeviceOSCache instance = new DeviceOSCache();

	private DeviceOSCache() {

		this.dataFile = new File(Configuration.getInstance()
				.getDownloadFilePath() + File.separator + "uaV3" + File.separator + "deviceos.csv");
		load();
	}

	public static DeviceOSCache getInstance() {
		return instance;
	}

	public void load() {

		if (!loading.getAndSet(true)
				&& Configuration.getInstance().switchUACache() && this.dataFile.exists()) {

			long start = System.currentTimeMillis();

			try {
				
				CsvReader reader = new CsvReader(this.dataFile.toString(), ',',
						Charset.forName("gbk"));

				while (reader.readRecord()) {

					if( reader.getCurrentRecord() == 0 )//设置列名
		    		{
		    			reader.setHeaders(reader.getValues());
		    		}
		    		else
		    		{
		    			DeviceOS os = new DeviceOS();
		    			os.setOs_id(NumberUtils.toInt(reader.get("OS_ID")));
		    			os.setOsname(StringUtils.remove(reader.get("OSNAME"),"(null)"));
		    			os.setOsvendor(StringUtils.remove(reader.get("OSVENDOR"),"(null)"));
		    			deviceOSMap.put(os.getOs_id(), os);
		    		}
				}
				
				reader.close();

			} catch (Exception e) {
				logger.error("DeviceOS信息加载异常", e);
			}

			loading.set(false);
			long end = System.currentTimeMillis();
			updateFileDownloadTime();
			logger.info("DeviceOS信息加载完成，time=" + (end - start) + "ms,size="
					+ deviceOSMap.size());

		} else {
			loading.set(false);
			logger.info("配置关闭，DeviceOS信息未加载");
		}
		
		loading.set(false);
	}

	@Override
	public void clear() {

		deviceOSMap.clear();
	}

	@Override
	public void reload() {

		if (this.fileIsChange()) {
			this.load();
		} else {
			logger.info("没变化，DeviceOS信息未加载");
		}
	}	
	
	public DeviceOS getDeviceOS( int osid )
	{
		DeviceOS os = this.deviceOSMap.get(osid);
//		if( os != null )
//		{			
//			try {
//				os = (DeviceOS)BeanUtils.cloneBean(os);
//			} catch (Exception e) {
//				logger.error("clone操作系统数据异常", e);
//			}
//		}
		
		return os;
	}

	public static void main(String[] args) {
		DeviceOSCache geoCache = DeviceOSCache.getInstance();
		System.out.println(geoCache.getDeviceOS(14));
	}
}